package com.haixinga.commbiz.commons.utils;

import com.haixinga.commbiz.commons.vo.BaseModel;
import org.apache.commons.lang3.StringUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateKit {

	public static final String FORMAT_DEFULT = "yyyy-MM-dd HH:mm:ss";
	public static final String FORMAT_DATE = "yyyy-MM-dd";
	public static final String FORMAT_APPEND = "yyyyMMddHHmmss";

	public static long getNowTimestamp() {

		return new Date().getTime();
	}

	public static String getNowTimeStr(){
		return getNowTime(FORMAT_DEFULT);
	}

	public static String getNowTime(String format){
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(new Date());
	}

	public static Date str2Date(String dateStr){
		return str2Date(dateStr, FORMAT_DEFULT);
	}

	public static Date str2Date(String dateStr, String format){
		try {
			SimpleDateFormat sdf = new SimpleDateFormat(format);
			return sdf.parse(dateStr);
		} catch (ParseException e) {
		}
		return null;
	}

	public static String date2Str(Date date){
		return date2Str(date, FORMAT_DEFULT);
	}


	public static String date2Str(Date date, String format){

		SimpleDateFormat sdf = new SimpleDateFormat(format);
		return sdf.format(date);

	}


	/** 获取时间偏移量 */
	public static Date getDateOffset(int offset){
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DATE, offset);
		return c.getTime();
	}

	/** 获取时间偏移量 toString */
	public static String getDateOffsetString(int offset, String format){
		return date2Str(getDateOffset(offset), format);
	}



	/**
	 * 获取自定义查询时间
	 * @return
	 */
	public static Date[] getQueryDateRange(String queryDateRange,
									   String queryStartDate, String queryEndDate,
									   String queryStartDateTime, String queryEndDateTime){

		Date resultBeginDateTime = null;
		Date resultEndDateTime = null;

		if(StringUtils.isNotEmpty(queryDateRange) && !"custom".equals(queryDateRange)){ //如果不为空 && 不是自定义

			Date nowDateTime = new Date();  //当前时间

			if("today".equals(queryDateRange)){
				resultBeginDateTime = DateUtil.getBegin(nowDateTime);
				resultEndDateTime = DateUtil.getEnd(nowDateTime);

			}else if("yesterday".equals(queryDateRange)){

				Date yesterdayDateTime = DateUtil.addDay(nowDateTime, -1); //昨天
				resultBeginDateTime = DateUtil.getBegin(yesterdayDateTime);
				resultEndDateTime = DateUtil.getEnd(yesterdayDateTime);


			}else if(queryDateRange.startsWith("near2now_")){  //近xx天， xx天之前 ~ 当前时间

				Integer offsetDay = 1 - Integer.parseInt(queryDateRange.substring(9));  //获取时间偏移量
				Date offsetDayDate = DateUtil.addDay(nowDateTime, offsetDay);

				resultBeginDateTime = DateUtil.getBegin(offsetDayDate);
				resultEndDateTime = DateUtil.getEnd(nowDateTime);

			}
			else if(queryDateRange.startsWith("near2yesterday_")){  //近xx天， xx天之前 ~ 昨天

				Date yesterdayDateTime = DateUtil.addDay(nowDateTime, -1); //昨天

				Integer offsetDay = 1 - Integer.parseInt(queryDateRange.substring(15));  //获取时间偏移量
				Date offsetDayDate = DateUtil.addDay(yesterdayDateTime, offsetDay);

				resultBeginDateTime = DateUtil.getBegin(offsetDayDate);
				resultEndDateTime = DateUtil.getEnd(yesterdayDateTime);
			}

			return new Date[]{resultBeginDateTime, resultEndDateTime};
		}

		if(StringUtils.isNotEmpty(queryStartDate)){
			resultBeginDateTime = DateUtil.str2date(queryStartDate + " 00:00:00");
		}
		if(StringUtils.isNotEmpty(queryEndDate)){
			resultEndDateTime = DateUtil.str2date(queryEndDate + " 23:59:59");
		}

		if(StringUtils.isNotEmpty(queryStartDateTime)){
			resultBeginDateTime = DateUtil.str2date(queryStartDateTime);
		}
		if(StringUtils.isNotEmpty(queryEndDateTime)){
			resultEndDateTime = DateUtil.str2date(queryEndDateTime);
		}

		return new Date[]{resultBeginDateTime, resultEndDateTime};

	}

	/** 获取年龄 **/
	public static Integer getBirthdayInt(Date birthday){

		if(birthday == null) return null;
		return Integer.parseInt(date2Str(new Date(), "yyyy")) - Integer.parseInt(date2Str(birthday, "yyyy"));
	}


	/** 方法重载 **/
	public static Date[] getQueryDateRange(BaseModel model){

		return getQueryDateRange(model.extv().getString("queryDateRange"),
				model.extv().getString("queryStartDate"), model.extv().getString("queryEndDate"),
				model.extv().getString("queryStartDateTime"), model.extv().getString("queryEndDateTime"));

	}



}
